#! /usr/bin/env python
#coding=utf-8


def need_notice(tablename):
    from uliweb import settings
#    settings = Ini('../apps/settings.ini')
    table_tmp = dict(settings.Audit.notice_tables)
#    default = False
    
#    return tablename in settings.get_var('Audit/notice_tables', [])
    return  tablename in table_tmp

def post_save(sender, instance, created, data, old_data):
    from uliweb.orm import get_model
    import pickle
    from datetime import datetime
    from uliweb import request
    from uliweb import settings, functions
    from uliweb.utils.common import Serial
    from uliweb.core.html import Tag
    from uliweb import settings
    from uliweb.mail import Mail
    
    tablename = sender.tablename
#    print 'che shi mmmmmmmmmmmmm', tablename
    if not need_notice(tablename):
        return
    
    notice_tables = dict(settings.Audit.notice_tables)
#    print 'che shi mmmmmmmmmmmmm'
    Notice = get_model('notice')
    Tables = get_model('tables')
    
    table = Tables.get_table(tablename)
#    print 'che shi mmmmmmmmmmmmm'
#    changed_value = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
    if created:
        modify_flag = '1'
        old_value = ''
        obj = Notice(table_id=table, obj_id=instance.id, sender=request.user,
            modify_flag=modify_flag, memo=unicode(instance))
            
        obj.save()
        
        #新增问题或风险给管理组发邮件
        if (table.table_name == 'question' or table.table_name == 'risk'):
            if table.table_name == 'question':
                t_title = u'[FLPM提醒]%s新提出问题:' % request.user.username
            if table.table_name == 'risk':
                t_title = u'[FLPM提醒]%s新提出风险:' % request.user.username
            str_link = notice_tables.get(table.table_name)
#            txt = str(u'新增|'+unicode(instance),str_link+str(instance.id))
            txt = str(Tag('a', "<font color='red'>"+u'新增|'+unicode(instance)+"</font>", href=settings.get_var('PARA/DOMAIN')+str_link+str(instance.id),target='_blank'))
            to_kfzx = settings.get_var('Q_R_KFZX_MAIL_RECEIVERS', {}).get(str(instance._kfzx_), [])
            to_tx = settings.get_var('Q_R_TX_MAIL_RECEIVERS', {}).get(str(instance._tx_), [])
            to_ = settings.get_var('PARA/ADM_TEAM') + to_kfzx + to_tx
            mail = {'from_':settings.get_var('PARA/EMAIL_SENDER'), 'to_':to_,
                'subject':t_title + unicode(instance) + '!', 'message':txt, 'html':True}
            _type = settings.get_var('PARA/NOTICE_TYPE', 'print')
            if _type == 'print':
                print mail
            elif _type == 'mail':
                Mail().send_mail(**mail)
            elif _type == 'redis':
                _t = Serial.dump(mail)
                redis = functions.get_redis()
                redis.lpush('send_mails', _t)
        
    else:
#        old = {}
        modify_flag = '2'
#        for k in data.iterkeys():
#            old[k] = old_data[k]
#        old_value = pickle.dumps(old, pickle.HIGHEST_PROTOCOL)
        
#    obj = Notice(table_id=table, obj_id=instance.id, sender=request.user,
#        modify_flag=modify_flag, memo=unicode(instance))
#        
#    obj.save()
    
#post_plan_save = post_save 
def pre_delete(sender, instance):
    from uliweb.orm import get_model
    import pickle
    from datetime import datetime
    from uliweb import request
    
    tablename = sender.tablename
    if not need_notice(tablename):
        return

    Notice = get_model('notice')
    Tables = get_model('tables')
    
    table = Tables.get_table(tablename)
#    print 'che shi ddddddddddddddddddd'
#    changed_value = ''
#    old_value = pickle.dumps(instance.to_dict(), pickle.HIGHEST_PROTOCOL)
    modify_flag = '3'
        
    obj = Notice(table_id=table, obj_id=instance.id, sender=request.user,
        modify_flag=modify_flag, memo=unicode(instance))
        
    obj.save()
    

def post_plan_save(sender, instance, created, data, old_data):
    from uliweb.orm import get_model
    import pickle
    from datetime import datetime
    from uliweb import request
    
    tablename = sender.tablename
#    print 'che shi mmmmmmmmmmmmm', tablename
    if not need_notice(tablename):
        return

#    print 'che shi mmmmmmmmmmmmm'
    Notice = get_model('notice')
    Tables = get_model('tables')
    
    table = Tables.get_table(tablename)
#    print 'che shi mmmmmmmmmmmmm'
#    changed_value = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
    if created:
        modify_flag = '1'
        old_value = ''
    else:
#        old = {}
        modify_flag = '2'
#        for k in data.iterkeys():
#            old[k] = old_data[k]
#        old_value = pickle.dumps(old, pickle.HIGHEST_PROTOCOL)
        
    obj = Notice(table_id=table, obj_id=instance.id, sender=request.user,
        modify_flag=modify_flag, memo=u'计划|'+unicode(instance))
        
    obj.save()
